package com.tobeface.sns.application;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kissme.lang.Preconditions;
import com.tobeface.sns.application.security.UserService;
import com.tobeface.sns.domain.User;
import com.tobeface.sns.domain.UserExistsException;

/**
 * 
 * @author loudyn
 * 
 */
@Service
public class RegisterServiceImpl implements RegisterService {
	private UserService userService;

	@Autowired
	public RegisterServiceImpl(UserService userService) {
		Preconditions.notNull(userService);

		this.userService = userService;
	}

	@Override
	public void register(User user) {
		userService.save(user.register());
	}

	@Override
	public void mustNotRegister(User user) {
		if (userService.existsByUsername(user.getMember().getUsername())) {
			throw new UserExistsException();
		}

	}

	@Override
	public boolean isUserExistsByUsername(String username) {
		return null == userService.getByUsername(username);
	}


	@Override
	public boolean isUserExistsByEmail(String email) {
		return null == userService.queryUniqueByEmail(email);
	}

	@Override
	public boolean isContactExistsByPhone(String phone) {
		return false;
	}

}
